package com.woniu.inlet.timer;

import com.woniu.adapter.CompanyWarnAdapter;
import com.woniu.outlet.dao.CompanyCurrentAccountMapper;
import com.woniu.outlet.dao.CompanyLoanMapper;
import com.woniu.outlet.dao.CompanyWarnMapper;
import com.woniu.outlet.po.CompanyCurrentAccount;
import com.woniu.outlet.po.CompanyLoan;
import com.woniu.outlet.po.CompanyWarn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//这个定时器用来监视对公客户的贷款的业务到期没有，如果到期，那么就加入到预警的信息中
@Configuration
@Slf4j
public class CompanyWarnTimer {

    @Autowired
    private CompanyLoanMapper companyLoanMapper;

    @Autowired
    private CompanyWarnMapper companyWarnMapper;

    @Autowired
    private CompanyCurrentAccountMapper companyCurrentAccountMapper;

    @Autowired
    private CompanyWarnAdapter companyWarnAdapter;

    //添加黑名单
    @Scheduled(fixedRate = 1000*60*60*24)//一天监视一次
    public void monitorloans() throws ParseException {
        //获取当前的系统的事件
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime1 = simpleDateFormat.format(new Date());
        Date nowtime = simpleDateFormat.parse(nowtime1);
        //System.out.println(nowtime);
        //获取数据库的到期时间
        List<CompanyLoan> companyLoanslist = companyLoanMapper.selectByExample(null);
        //System.out.println(companyLoanslist);
        if(companyLoanslist.size()!=0){
            //循环集合
            for(CompanyLoan companyLoan:companyLoanslist){
                Date companyloan_endtime = companyLoan.getCompanyloanEndtime();
                if(nowtime.getTime()>companyloan_endtime.getTime()){
                    //那么贷款已经超时了，加入到黑名单
                    CompanyWarn companyWarn = new CompanyWarn();
                    companyWarn.setCompanyId(companyLoan.getCompanyId());//黑名单为1
                    companyWarn.setCompanywarnActive(1);
                    companyWarnMapper.insertSelective(companyWarn);
                }

            }
        }

    }

    //添加存款红线
    @Scheduled(fixedRate = 1000*60*60*24)
    public void monitorregular() throws ParseException {
        //获取当前的系统的事件
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowtime1 = simpleDateFormat.format(new Date());
        Date nowtime = simpleDateFormat.parse(nowtime1);
        //获取对公活期存款的余额
        List<CompanyCurrentAccount> companyCurrentAccounts = companyCurrentAccountMapper.selectByExample(null);
        if(companyCurrentAccounts.size()>0){
            //循环集合
            for(CompanyCurrentAccount companyCurrentAccount:companyCurrentAccounts){
                //取出余额
                Integer companycurrentBalance = companyCurrentAccount.getCompanycurrentBalance();
                if(companycurrentBalance<1000){
                    //存款已经到了红线
                    CompanyWarn companyWarn = new CompanyWarn();
                    companyWarn.setCompanyId(companyCurrentAccount.getCompanyId());
                    companyWarn.setCompanywarnActive(0);//存款红线为0
                    companyWarnMapper.insertSelective(companyWarn);
                }

            }
        }

    }

    //这边可以定义一个定时器，然后监视warn表格，随后发送消息到队列
    @Scheduled(fixedRate = 1000*60*60*24)
    public void aaa(){
        //监听warn表里面的所有的信息
        List<CompanyWarn> companyWarns = companyWarnMapper.selectByExample(null);
        companyWarnAdapter.warn(companyWarns);
    }



}
